package com.xnx3.banmahuishou.agency.controller;

import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xnx3.DateUtil;
import com.xnx3.banmahuishou.core.entity.OrderEvaluationRecord;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.util.ActionLogUtil;
import com.xnx3.j2ee.util.Page;
import com.xnx3.j2ee.util.Sql;
import com.xnx3.j2ee.vo.BaseVO;
import com.xnx3.wangmarket.shop.core.entity.Order;
import com.xnx3.wangmarket.shop.core.entity.OrderGoods;
import com.xnx3.wangmarket.shop.core.service.OrderService;
import com.xnx3.wangmarket.shop.core.service.OrderStateLogService;
import com.xnx3.wangmarket.shop.core.vo.OrderVO;
import com.xnx3.wangmarket.shop.store.vo.OrderListVO;

/**
 * 订单管理
 * @author 王晓龙
 */
@Controller(value="BanmahuishouAgencyOrderController")
@RequestMapping("/agency/order/")
public class OrderController extends BaseController  {
	@Resource
	private SqlService sqlService;
	@Resource
	private OrderService orderService;
	@Resource
	private OrderStateLogService orderStateLogService;

	/**
	 * 查看订单列表
	 * @param startTime 日期范围的开始时间
	 * @param endTime 日期范围的结束时间
	 * @author 王晓龙
	 */
	@ResponseBody
	@RequestMapping(value="list.json", method = {RequestMethod.POST})
	public OrderListVO list(HttpServletRequest request,Model model,
			@RequestParam(value = "startTime", required = false, defaultValue = "0") int startTime,
			@RequestParam(value = "endTime", required = false, defaultValue = "0") int endTime) {
		OrderListVO vo = new OrderListVO();
		//创建Sql
		Sql sql = new Sql(request);
		//配置查询那个表
		sql.setSearchTable("shop_order");

		//配置按某个字端搜索内容
		sql.setSearchColumn(new String[] {"qishou_name=","no","phone=","state="});
		sql.appendWhere("((reserve_end_time > " + DateUtil.timeForUnix10() + " AND state = " + Order.STATE_DAIQUHUO + ") OR state = " + Order.STATE_FINISH + " OR state = " + Order.STATE_CANCEL +  " OR state = " + Order.STATE_EVALUATED + ") AND dailishang_userid = " + getUserId());
//		String addtime = request.getParameter("addtime");
//		//下单时间NaN拦截
//		if (StringUtils.hasLength(addtime) && Objects.equals(addtime, "NaN")) {
//			vo.setBaseVO(BaseVO.FAILURE,"下单时间错误,请刷新后重新选择");
//			return vo;
//		}
//		// 只传入开始时间
//		if (StringUtils.hasLength(addtime)) {
//			//传入指定的时间返回明天00:00:00的时间戳
//			int dayEndTime = DateUtil.getDateZeroTime(Integer.valueOf(addtime))+24*60*60;
//			sql.appendWhere("addtime >= " + addtime + " AND addtime < " + dayEndTime);
//		}
		//时间查询条件
		if (startTime > 0) {
			sql.appendWhere(" addtime >= " + startTime);
		}
		if (endTime > 0) {
			//传入指定的时间返回明天00:00:00的时间戳
			int dayEndTime = DateUtil.getDateZeroTime(endTime)+24*60*60;
			sql.appendWhere(" addtime <= " + dayEndTime);
		}
		// 查询数据表的记录总条数
		int count = sqlService.count("shop_order", sql.getWhere());
		
		// 配置每页显示15条
		Page page = new Page(count, 15, request);
		// 查询出总页数
		sql.setSelectFromAndPage("SELECT * FROM shop_order ", page);
		//选择排序方式 当用户没有选择排序方式时，系统默认降序排序
		sql.setDefaultOrderBy("id DESC");

		// 按照上方条件查询出该实体总数 用集合来装
		List<Order> list = sqlService.findBySql(sql,Order.class);
		
		// 将信息保存到vo中
		vo.setList(list);
		vo.setPage(page);
		//日志记录
		ActionLogUtil.insert(request, getUserId(), "查看订单列表");
		return vo;
	}
	
	/**
	 * 查看异常订单列表
	 * @author 王晓龙
	 */
	@ResponseBody
	@RequestMapping(value="notTakenList.json", method = {RequestMethod.POST})
	public OrderListVO notTakenList(HttpServletRequest request,Model model) {
		OrderListVO vo = new OrderListVO();
		//创建Sql
		Sql sql = new Sql(request);
		//配置查询那个表
		sql.setSearchTable("shop_order");

		//配置按某个字端搜索内容
		sql.setSearchColumn(new String[] {"qishou_name"});
		// 状态是待接单  或者 状态是待取货 且 已超过预约时间
		sql.appendWhere("((reserve_end_time < " + DateUtil.timeForUnix10() + " AND state = " + Order.STATE_DAIQUHUO + ") OR state = " + Order.STATE_DAIJIEDAN + ") AND dailishang_userid = " +getUserId());
		// 查询数据表的记录总条数
		int count = sqlService.count("shop_order", sql.getWhere());

		// 配置每页显示15条
		Page page = new Page(count, 15, request);
		// 查询出总页数
		sql.setSelectFromAndPage("SELECT * FROM shop_order ", page);
		//选择排序方式 当用户没有选择排序方式时，系统默认降序排序
		sql.setDefaultOrderBy("id DESC");

		// 按照上方条件查询出该实体总数 用集合来装
		List<Order> list = sqlService.findBySql(sql,Order.class);

		// 将信息保存到vo中
		vo.setList(list);
		vo.setPage(page);
		//日志记录
		ActionLogUtil.insert(request, getUserId(), "查看订单列表");
		return vo;
	}

	/**
	 * 查看订单详情
	 * @author 王晓龙
	 * @param id 订单id
	 */
	@ResponseBody
	@RequestMapping(value="orderDetails.json", method = {RequestMethod.POST})
	public OrderVO orderDetails(Model model ,HttpServletRequest request,
							   @RequestParam(value = "id", required = false, defaultValue = "0") int id) {
		
		OrderVO vo = new OrderVO();
		if(id > 0) {
			//查到订单信息
			Order order = sqlService.findById(Order.class, id);
			//查看订单的商品信息
			String sql = "SELECT * FROM shop_order_goods WHERE orderid = " + id;
			List<OrderGoods> list = sqlService.findBySqlQuery(sql,OrderGoods.class);
			//查看订单评价记录
			OrderEvaluationRecord orderEvaluationRecord = sqlService.findById(OrderEvaluationRecord.class, id);
			
			vo.setGoodsList(list);
			vo.setOrder(order);
			vo.setOrderEvaluationRecord(orderEvaluationRecord);
			ActionLogUtil.insert(request, getUserId(), "查看订单ID为" + id+ "的详情");
			
		}
		return vo;
	}
}
